const { ipcMain, shell } = require('electron');
const exec = require('child_process').exec;
const xlsx = require("./xlsx");
const { getChanDaoData,watchPublicInfo } = require("./syncData");
// 监听设置大小
function watchSetSize(win) {
    ipcMain.on("setSize", (event, setObj) => {
        win.setSize(setObj.width, setObj.height);
    })
}
// 监听读取文件
function watchReadFile(win) {
    ipcMain.on("readFile", (event, setObj) => {
        event.sender.send("returnFileData", xlsx.readFile())
    })
}
// 监听读取文件
function watchOpenUrl(win) {
    ipcMain.on("openUrl", (event, setObj) => {
        openDefaultBrowser(setObj.url)
        event.sender.send("isOpenUrl", '1')
    })
}

// 监听过滤
function watchFilterFile(win) {
    ipcMain.on("filterFile", async (event, setObj) => {
        const { filterStr } = setObj;
        const baseData = xlsx.readFile();
        baseData[0].data.forEach(trItem => {
            if (trItem.some(tdItem => {
                return String(tdItem).includes(filterStr)
            })) {
                trItem[5] = true
            } else {
                trItem[5] = false
            }
        })
        event.sender.send("returnFileData", baseData)
        xlsx.setFile(baseData[0].data);
    })
}
// 监听禅道同步信息
function watchChaoDaoMessage() {
    ipcMain.on("getChanDaoData", async (event, setObj) => {
        try {
            const { mode, zentaosid } = setObj;
            await getChanDaoData({ mode, zentaosid })
            const baseData = xlsx.readFile();
            event.sender.send("returnFileData", baseData)
            event.sender.send("closeLoading")
            xlsx.setFile(baseData[0].data);
        } catch (err) {
            event.sender.send("errorLoading")
        }
    })
}
function watchWatchPublicInfo(){
    ipcMain.on("watchPublicInfo", async (event, setObj) => {
        try {
            const { mode, publish_system_sess_,packageId } = setObj;
            await watchPublicInfo({ mode, publish_system_sess_, packageId,event })
        } catch (err) {
            event.sender.send("errorLoading")
        }
    })
}
// 监听设置文件
function watchSetupFile() {
    function toSetFile(setObj) {
        setObj.dataList[0].data.sort((a, b) => { return new Date(a[2]) - new Date(b[2]) });
        xlsx.setFile(setObj.dataList[0].data);
    }
    ipcMain.on("setupFile", (event, setObj) => {
        if (setObj.type === "add") {
            if (!setObj.nameValue) return;
            if (!setObj.linkValue) return;
            if (!setObj.startDate) return;
            if (!setObj.type) return;
        }
        if (!setObj.id) return;
        switch (setObj.type) {
            case "add":
                setObj.dataList = xlsx.readFile()
                setObj.dataList[0].data.push([setObj.nameValue, setObj.linkValue, setObj.startDate, "", setObj.id, true])
                toSetFile(setObj);
                break;
            case "complete":
                const findItem = setObj.dataList[0].data.find(item => {
                    return item[4] == setObj.id
                });
                if (findItem) {
                    const nowDate = new Date();
                    findItem[3] = `${nowDate.getFullYear()}/${nowDate.getMonth() + 1}/${nowDate.getDate()}`;
                    toSetFile(setObj);
                }
                break;
            case "edit":
                const editItem = setObj.dataList[0].data.find(item => {
                    return item[4] == setObj.id
                });
                if (editItem) {
                    if (setObj.nameValue && setObj.linkValue && setObj.startDate) {
                        editItem[0] = setObj.nameValue
                        editItem[1] = setObj.linkValue
                        editItem[2] = setObj.startDate
                        toSetFile(setObj);
                    };
                }
                break;
            case "del":
                setObj.dataList[0].data = setObj.dataList[0].data.filter(item => {
                    return item[4] != setObj.id
                });
                toSetFile(setObj)
                break;
            case "reset":
                const resetItem = setObj.dataList[0].data.find(item => {
                    return item[4] == setObj.id
                });
                if (resetItem) {
                    resetItem[3] = "";
                    toSetFile(setObj);
                }
                break;
            case "upHierarchicalStructure":
                const upHierarchicalStructureItem = setObj.dataList[0].data.find(item => {
                    return item[4] == setObj.id
                });
                if (upHierarchicalStructureItem) {
                    // 设置为优先处理
                    if(upHierarchicalStructureItem[6] == 1){
                        delete upHierarchicalStructureItem[6] 
                    }else{
                        upHierarchicalStructureItem[6] = 1
                    }
                    toSetFile(setObj);
                }
        }
        event.sender.send("returnFileData", xlsx.readFile())
    })
}
function toWatchIng(win) {
    watchSetSize(win);
    watchReadFile(win);
    watchFilterFile(win);
    watchSetupFile(win);
    watchOpenUrl(win);
    watchChaoDaoMessage(win);
    watchWatchPublicInfo(win);
}
/**
 * 通过默认浏览器打开地址
 * @param {string} url 浏览器打开地址
 */
function openDefaultBrowser(url) {
    shell.openExternal(url)
}
module.exports = {
    toWatchIng
}